package com.baizhi.controller;

import com.baizhi.entity.User;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController //代表接口中返回都是json格式数据
@CrossOrigin //运行所有请求 所有域访问 解决:跨域问题
public class AdminController {

    //定义post方式接口
    @PostMapping("user")
    public Map<String,Object> save(@RequestBody User user){
        System.out.println("user: "+user);
        HashMap<String, Object> result = new HashMap<>();
        result.put("success",true);
        result.put("msg","添加成功~~");
        return result;
    }


    //rest接口  url/11/
    @GetMapping("user/{id}")
    public User findUserById(@PathVariable("id") Integer id){
        System.out.println("id: "+id);
        System.out.println("user ...");
        return new User(id,"小三",23,new Date());
    }


    //queryString接口 url?id=11
    @GetMapping("user")
    public User user(@RequestParam("id") Integer id){
        System.out.println("id: "+id);
        System.out.println("user ...");
        return new User(id,"小陈",23,new Date());
    }


    //测试接口
    @GetMapping("demo")
    public String demo(){
        System.out.println("demo ....");
        return "demo ok ";
    }
}
